package com.base.cn.platform.os.controller.course.cou.buy;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.entity.course.cou.buy.CourseBuyPackageCourse;
import com.base.cn.platform.os.entity.course.cou.packageCou.PackageCourse;
import com.base.cn.platform.os.service.course.cou.buy.CourseBuyPackageCourseBiz;
import com.base.cn.platform.os.service.course.cou.packageCou.PackageCourseBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 套餐激活列表
 *
 * @author s.li
 * @create 2018-05-28-10:35
 */
@RestController
@RequestMapping("/course")
public class CourseBuyPackageCourseController extends BaseController {

    @Autowired
    private CourseBuyPackageCourseBiz courseBuyPackageCourseBiz;
    @Autowired
    private PackageCourseBiz packageCourseBiz;

    /**
     * 自动激活套餐课程
     * @param orderId 订单ID
     * @param orderNo 订单号
     * @param packageIds 套餐课程ID串
     * @param userId 用户ID
     */
    @RequestMapping("/autoActivation")
    public void autoActivation(@RequestParam("orderId") BigDecimal orderId,
                                @RequestParam("orderNo") String orderNo,
                               @RequestParam ("packageIds") String packageIds,
                               @RequestParam("userId") BigDecimal userId){
        PackageCourse packageCourse = new PackageCourse();
        packageCourse.setPackageIds(packageIds);
        List<PackageCourse> packageCourseList = packageCourseBiz.findList(packageCourse ,false);
        if(ObjectUtils.isNotEmpty(packageCourseList)){
            List<CourseBuyPackageCourse> courseBuyPackageCourseList = new ArrayList<>();
            packageCourseList.stream().filter(e-> DataUtil.idIsNotNull(e.getCourseId())).forEach(e->{
                courseBuyPackageCourseList.add(courseBuyPackageCourseBiz.getCourseBuyPackageCourse(orderId,orderNo,userId,e.getPackageId(),e.getCourseId()));
            });
            if(ObjectUtils.isNotEmpty(courseBuyPackageCourseList)){
                courseBuyPackageCourseBiz.createCourseBuyPackageCourse(courseBuyPackageCourseList);
            }
        }
    }


    /**
     * 获取学员专业科目
     * @param userId
     * @return
     */
    @RequestMapping("/getUserSubjectIds")
    String getUserSubjectIds( @RequestParam("userId") BigDecimal userId){
        String subjectIds = courseBuyPackageCourseBiz.getUserSubjectIds(userId);
        return subjectIds;
    }
}
